package dao;

import beans.GeneralTo;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import utildb.GenericDAO;
import utildb.Transaction;
import utildb.TransactionException;


public class GeneralDAO extends GenericDAO {
    
    public GeneralDAO(Transaction transacction) {
        super(transacction);
    }

    public void create(GeneralTo to) throws TransactionException {
            try {
                    int i = 1;
                    Connection c;
                    PreparedStatement statement = null;
                    c = (Connection) transaction.getConnection();

                    statement = c.prepareStatement("insert into mySql.GEMC_Gene " +
                                 " (nnum_visi, npor_IGV, cNum_Seri, "
                                + " nNum_Docu, nPor_Incr_Prec, cNum_Cta_Cte, "
                                + " daud_Fec_Modi, caud_Usu_Modi, daud_Fec_Crea, caud_Usu_Crea ) "
                                + "values (?,?,?,?,?, ?,?,?,?,? )");
                                        // 1 2 3 4 5  6 7 8 9 10
                    statement.setInt(i++, to.getnNum_Visi());
                    statement.setDouble(i++, to.getnPor_IGV());
                    statement.setString(i++, to.getcNum_Seri());
                    statement.setInt(i++, to.getnNum_Docu());
                    statement.setDouble(i++, to.getnPor_Incr_Prec());
                    statement.setString(i++, to.getcNum_Cta_Cte());
                    //
                    statement.setDate(i++, to.getDaud_Fec_Modi());
                    statement.setString(i++, to.getCaud_Usu_Modi());
                    statement.setDate(i++, to.getDaud_Fec_Crea());
                    statement.setString(i++, to.getCaud_Usu_Crea());
                
                    int n = statement.executeUpdate();
                    statement.close();
                    if (n == 0)
                            throw new TransactionException("No se pudo crear el producto");
            } catch (SQLException e1) {
                    throw new TransactionException(e1);
            }
    }

    public void eliminar(String codProd ) throws TransactionException {
           throw new TransactionException("No soportado");
    }

    public GeneralTo buscarGeneral() throws TransactionException {
            Connection c;
            Statement statement = null;
            GeneralTo general = null;
            //
            Integer nNum_Visi;
            Double nPor_IGV;
            Double nPor_Incr_Prec;
            String cNum_Cta_Cte;
            String ccadSql = "";
            try {
                    c = transaction.getConnection();
                    Integer tipobd = transaction.getTipobd();
                    statement = c.createStatement();
                    ccadSql = "select * from mySql.vemc_Gene Order By 1";
                    ResultSet rs = statement.executeQuery( ccadSql );

                    int haydatos = 0;
                    while (rs.next()) {
                        haydatos = 1;
                        nNum_Visi = rs.getInt("nNum_Visi");
                        nPor_IGV  = rs.getDouble("nPor_IGV");
                        nPor_Incr_Prec= rs.getDouble("nPor_Incr_Prec");
                        cNum_Cta_Cte  = rs.getString("cNum_Cta_Cte");
                        general = new GeneralTo( nNum_Visi, nPor_IGV,
                                null, null, nPor_Incr_Prec,
                                cNum_Cta_Cte );
                    }
                    rs.close();
                    statement.close();
                    if (haydatos == 0) {
                        general = new GeneralTo( null, 19.00,
                                null, null, null, null );
                    }
            } catch (SQLException e1) {
                    throw new TransactionException(e1);
            }
            return general;
    }
    
}
